﻿using System;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;

namespace LAN_IP_ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Unhandled exception. System.PlatformNotSupportedException: The information requested is unavailable on the current platform.
            var ip4 = Local_IP4();
            Console.WriteLine($"ip4:{ip4}");

            //Unhandled exception. System.TypeInitializationException: The type initializer for 'LAN_IP_ConsoleApp.Program' threw an exception.
            //--->System.PlatformNotSupportedException: System.Management currently is only supported for Windows desktop applications.
            var ipv4 = Local_IPv4();
            Console.WriteLine($"ipv4:{ipv4}");


            //var linux_ip = linux_IP(); 
            //Console.WriteLine($"linux_ip:{linux_ip}"); // ::1,127.0.0.1

        }

        public static string Local_IP4()
        {
            var ipv4 = string.Empty;
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var all_interface = NetworkInterface.GetAllNetworkInterfaces().Where(t => t.OperationalStatus == OperationalStatus.Up);
                foreach (var itf in all_interface)
                {
                    var gatewayAddrs = itf.GetIPProperties().GatewayAddresses;
                    var unicastAddrs = itf.GetIPProperties().UnicastAddresses;

                    if (gatewayAddrs.Count > 0 && gatewayAddrs[0].Address.ToString() != "0.0.0.0")
                    {
                        ipv4 = unicastAddrs.Where(t => t.IsDnsEligible).FirstOrDefault().Address.ToString();
                        break;
                    }
                }
            }
            return ipv4;
        }

        private static ManagementObjectSearcher moSch = new ManagementObjectSearcher("root\\CIMV2", "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration where IPEnabled = True and DHCPEnabled = True");

        public static string Local_IPv4()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var moc = moSch.Get();

                //if (moc.Count > 1)
                //{
                //    throw new ApplicationException("The default IPv4 address could not be determined as there are two interfaces with gateways.");
                //}

                foreach (var mo in moc)
                {
                    var strs = mo.GetPropertyValue("IPAddress") as string[];
                    if (strs == null || strs.Length <= 0)
                    {
                        return string.Empty;
                    }
                    else
                    {
                        return strs[0];
                    }
                }
            }
            return string.Empty;
        }

        //public static string linux_IP()
        //{
        //    var host_addrs = Dns.GetHostAddresses(Dns.GetHostName());

        //    return string.Join(",", host_addrs.Select(t => t.ToString()));
        //}
    }
}
